package com.ruoyi.quartz.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.quartz.domain.SysJobLog;
import com.ruoyi.quartz.mapper.SysJobLogMapper;
import com.ruoyi.quartz.service.ISysJobLogService;

/**
 * 定时任务调度日志信息 服务层
 * 
 * @author ruoyi
 */
@Service
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog>  implements ISysJobLogService
{
    @Autowired
    private SysJobLogMapper jobLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     * 
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog)
    {
        LambdaQueryWrapper<SysJobLog> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(jobLog.getJobLogId() != null,SysJobLog::getJobLogId,jobLog.getJobLogId());
        queryWrapper.like(StringUtils.isNotBlank(jobLog.getJobName()),SysJobLog::getJobName,jobLog.getJobName());
        queryWrapper.like(StringUtils.isNotBlank(jobLog.getJobGroup()),SysJobLog::getJobGroup,jobLog.getJobGroup());
        queryWrapper.like(StringUtils.isNotBlank(jobLog.getInvokeTarget()),SysJobLog::getInvokeTarget,jobLog.getInvokeTarget());
        queryWrapper.like(StringUtils.isNotBlank(jobLog.getJobMessage()),SysJobLog::getJobMessage,jobLog.getJobName());
        queryWrapper.eq(StringUtils.isNotBlank(jobLog.getStatus()),SysJobLog::getStatus,jobLog.getStatus());
        try{
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date beginTime = jobLog.getParams().get("beginTime")==null ? null: formatter.parse(String.valueOf(jobLog.getParams().get("beginTime")));
            Date endTime = jobLog.getParams().get("endTime")==null ? null: formatter.parse(String.valueOf(jobLog.getParams().get("endTime")));
            queryWrapper.ge(beginTime != null,SysJobLog::getCreateTime, beginTime);
            queryWrapper.le(endTime!=null,SysJobLog::getCreateTime,endTime);
        }catch (Exception e){
            throw new RuntimeException(e.getMessage());
        }
        queryWrapper.orderBy(true,true,SysJobLog::getCreateTime);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     * 
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId)
    {
        return jobLogMapper.selectJobLogById(jobLogId);
    }

    /**
     * 新增任务日志
     * 
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog)
    {
        jobLogMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     * 
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds)
    {
        return jobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     * 
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId)
    {
        return jobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog()
    {
        jobLogMapper.cleanJobLog();
    }
}
